package com.cinc.ebc.pp.workOrder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cinc.ebc.mm.entity.MmPurchaseRequisition;
import com.cinc.ebc.pp.workOrder.entity.PpWorkOrder;
import com.cinc.ebc.pp.workOrder.mapper.PpWorkOrderMapper;
import com.cinc.ebc.pp.workOrder.service.IPpWorkOrderService;
import com.cinc.ebc.pp.workOrderProcess.entity.PpWorkOrderProcess;
import com.cinc.ebc.pp.workOrderProcess.service.IPpWorkOrderProcessService;
import com.cinc.ebc.pp.workOrderReq.entity.PpWorkOrderReq;
import com.cinc.ebc.pp.workOrderReq.service.IPpWorkOrderReqService;
import com.cinc.mdm.modules.bom.entity.MdmBom;
import com.cinc.mdm.modules.bom.service.IMdmBomService;
import com.cinc.mdm.modules.bom.vo.MdmBomTreePage;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRoute;
import com.cinc.mdm.modules.processRoute.entity.MdmProcessRouteDetail;
import com.cinc.mdm.modules.processRoute.service.IMdmProcessRouteDetailService;
import com.cinc.mdm.modules.processRoute.service.IMdmProcessRouteService;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @Description: 生产工单
 * @Author: jeecg-boot
 * @Date:   2024-04-04
 * @Version: V1.0
 */
@Service
public class PpWorkOrderServiceImpl extends ServiceImpl<PpWorkOrderMapper, PpWorkOrder> implements IPpWorkOrderService {

    @Autowired
    private IMdmBomService mdmBomService;

    @Autowired
    private IMdmProcessRouteService mdmProcessRouteService;

    @Autowired
    private IMdmProcessRouteDetailService mdmProcessRouteDetailService;

    @Autowired
    private IPpWorkOrderReqService ppWorkOrderReqService;

    @Autowired
    private IPpWorkOrderProcessService ppWorkOrderProcessService;


    @Transactional
    @Override
    public void confirm(PpWorkOrder ppWorkOrder) {
        PpWorkOrder data = baseMapper.selectById(ppWorkOrder.getId());
        if(!"10".equals(data.getStatus())){
            throw new RuntimeException("已经确认，不能重复确认");
        }
        if(oConvertUtils.isEmpty(data.getMaterialId())){
            throw new RuntimeException("产品信息不能为空");
        }
        if(data.getOrderQty() == null || data.getOrderQty().compareTo(BigDecimal.ZERO) <= 0){
            throw new RuntimeException("工单数量必须大于零");
        }
        // 工单确认
        PpWorkOrder ppWorkOrderConifrm = new PpWorkOrder();
        ppWorkOrderConifrm.setId(ppWorkOrder.getId());
        ppWorkOrderConifrm.setStatus("11");
        baseMapper.updateById(ppWorkOrderConifrm);

        // 根据工艺路线生成加工明细
        List<MdmProcessRouteDetail> routeDetailList = new ArrayList<>();
        LambdaQueryWrapper<MdmProcessRoute> processRouteQuery = new LambdaQueryWrapper<>();
        processRouteQuery.eq(MdmProcessRoute::getMaterialId, data.getMaterialId());
        processRouteQuery.eq(MdmProcessRoute::getEffectFlag, "1");
        List<MdmProcessRoute> processRouteList = mdmProcessRouteService.list(processRouteQuery);
        if(oConvertUtils.listIsNotEmpty(processRouteList)){
            routeDetailList = mdmProcessRouteDetailService.selectByMainId(processRouteList.get(0).getId());
        }
        if(oConvertUtils.listIsEmpty(routeDetailList)){
            throw new RuntimeException("请先维护产品工艺路线");
        }
        routeDetailList.sort(Comparator.comparing(MdmProcessRouteDetail::getSeqNo));
        int firstSeqNo = routeDetailList.get(0).getSeqNo();
        for(int i=0; i<routeDetailList.size(); i++){
            MdmProcessRouteDetail routeDetail = routeDetailList.get(i);
            PpWorkOrderProcess workOrderProcess = new PpWorkOrderProcess();
            workOrderProcess.setId(UUIDGenerator.generate());
            workOrderProcess.setWorkOrderId(ppWorkOrder.getId());
            workOrderProcess.setRouteDetailId(routeDetail.getId());
            workOrderProcess.setSeqNo(routeDetail.getSeqNo());
            workOrderProcess.setProcessNo(routeDetail.getProcessNo());
            workOrderProcess.setProcessName(routeDetail.getProcessName());
            workOrderProcess.setWorkId(routeDetail.getWorkId());
            workOrderProcess.setStandardTime(routeDetail.getStandardHours());
            workOrderProcess.setPrepareTime(routeDetail.getPrepareHours());
            workOrderProcess.setWaitTime(routeDetail.getWaitHours());
            workOrderProcess.setTimeUnit(routeDetail.getUnit());
            if(routeDetail.getSeqNo()==firstSeqNo){
                workOrderProcess.setStatus("02");
                workOrderProcess.setActStartDate(new Date());
            }else{
                workOrderProcess.setStatus("01");
            }
            ppWorkOrderProcessService.save(workOrderProcess);
        }

        // 根据bom生成物料需求
        MdmBomTreePage bom = mdmBomService.queryTree(data.getMaterialId(), data.getOrderQty());
        if(bom == null || oConvertUtils.listIsEmpty(bom.getChildren())){
            throw new RuntimeException("请先维护产品BOM信息");
        }
        List<MdmBomTreePage> bomList = bom.getChildren();
        while(oConvertUtils.listIsNotEmpty(bomList)){
            List<MdmBomTreePage> tempList = new ArrayList<>();
            for(MdmBomTreePage bomData : bomList){
                PpWorkOrderReq workOrderReq = new PpWorkOrderReq();
                workOrderReq.setId(UUIDGenerator.generate());
                workOrderReq.setMaterialId(bomData.getMaterialId());
                workOrderReq.setMaterialNo(bomData.getMaterialCode());
                workOrderReq.setMaterialDesc(bomData.getMaterialDesc());
                workOrderReq.setPerQty(bomData.getPartQty());
                workOrderReq.setReqQty(bomData.getSumQty());
                workOrderReq.setUnit(bomData.getMaterialUnitCode());
                workOrderReq.setWorkOrderId(ppWorkOrder.getId());
                ppWorkOrderReqService.save(workOrderReq);
                //
                if(oConvertUtils.listIsNotEmpty(bomData.getChildren())){
                    tempList.addAll(bomData.getChildren());
                }
            }
            bomList = tempList;
        }
    }

    @Override
    public void close(PpWorkOrder ppWorkOrder) {
        PpWorkOrder data = baseMapper.selectById(ppWorkOrder.getId());
        if(!"11".equals(data.getStatus())){
            throw new RuntimeException("当前状态不能关闭");
        }
        PpWorkOrder ppWorkOrderClose = new PpWorkOrder();
        ppWorkOrderClose.setId(ppWorkOrder.getId());
        ppWorkOrderClose.setStatus("99");
        baseMapper.updateById(ppWorkOrderClose);
    }

    @Override
    public void open(PpWorkOrder ppWorkOrder) {
        PpWorkOrder data = baseMapper.selectById(ppWorkOrder.getId());
        if(!"99".equals(data.getStatus())){
            throw new RuntimeException("当前状态不能开启");
        }
        PpWorkOrder ppWorkOrderClose = new PpWorkOrder();
        ppWorkOrderClose.setId(ppWorkOrder.getId());
        ppWorkOrderClose.setStatus("11");
        baseMapper.updateById(ppWorkOrderClose);
    }
}
